﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;

namespace TractRecord
{
    public class Global : System.Web.HttpApplication
    {

        protected void Application_Start(object sender, EventArgs e)
        {
            SiteMap.SiteMapResolve +=
            new SiteMapResolveEventHandler(SiteMapResolveEvent);    
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        public static SiteMapNode SiteMapResolveEvent(object sender, SiteMapResolveEventArgs e)
        {
            SiteMapNode node = null;
                        
            node = HandleUnmappedNodes();

            node = HideRootNode(node);

            return node;
        }

        static SiteMapNode HandleUnmappedNodes()
        {
            HttpContext context = HttpContext.Current;

            // Create a custom SiteMapNode for Unmapped.aspx.
            if (context.Request.Path.ToLower().Contains("project.aspx"))
            {
                string param = context.Request["id"];
                string name = TractRecord.Code.DataAccess.ProjectName(param);

                string title = String.IsNullOrEmpty(name) ?
                    "Untitled Project" : name;
                SiteMapNode node = new SiteMapNode(SiteMap.Provider,
                    "/Authenticated/Projects/Project.aspx", "/Authenticated/Projects/Project.aspx", title);
                node.ParentNode = SiteMap.Provider.FindSiteMapNode("/Authenticated/Projects/Projects.aspx");
                return node;
            }

            return null; // Do nothing for other URLs.
        }

        static SiteMapNode HideRootNode(SiteMapNode unmappednode)
        {
            //
            // Hide the root node from SiteMapPath controls by cloning the site
            // map from the current node up to the node below the root node and
            // setting that node's ParentNode property to null.
            //
            SiteMapNode node = SiteMap.CurrentNode != null ? SiteMap.CurrentNode.Clone() : unmappednode;
            SiteMapNode current = node;
            SiteMapNode root = SiteMap.RootNode;
            if (current != null)
            {
                if (current.Key != root.Key) // Just in case the current node *is* the root node!
                {
                    while (node.ParentNode != root)
                    {
                        node.ParentNode = node.ParentNode.Clone();
                        node = node.ParentNode;
                    }
                    node.ParentNode = null;
                }
            }
            return current;
        }
    }
}